﻿package component.fileupload.controller;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import component.fileupload.action.FileDownloadAction;
import component.fileupload.action.IFileDownloadAction;
import component.fileupload.exception.FileUploadException;

public class DownloadServlet extends HttpServlet {
	private IFileDownloadAction downloadAction = new FileDownloadAction();

	private ServletConfig servletConfig;

	public ServletConfig getServletConfig() {
		// TODO Auto-generated method stub
		return servletConfig;
	}

	public String getServletInfo() {
		// TODO Auto-generated method stub
		return servletConfig.getServletName();
	}

	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
		this.servletConfig = config;
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		String filePath = request.getParameter("filePath"); // 相对路径
		String fileName = request.getParameter("fileName"); // 文件名称,包括后缀名
		try {
			downloadAction.doDownload(servletConfig.getServletContext(),
					request, response, filePath, fileName);
		} catch (FileUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
